package com.bugsnag.ohos;

import java.io.IOException;
import java.util.List;

/**
 * ThreadInternal
 *
 * @since 2021-07-06
 */
public class ThreadInternal implements JsonStream.Streamable {
    long id;
    String name;
    ThreadType type;
    boolean isErrorReportingThread;
    List<Stackframe> stacktrace;

    /**
     * constructor
     *
     * @param id
     * @param name
     * @param type
     * @param isErrorReportingThread
     * @param stacktrace
     */
    public ThreadInternal(long id, String name, ThreadType type,
                          boolean isErrorReportingThread, Stacktrace stacktrace) {
        this.id = id;
        this.name = name;
        this.type = type;
        this.isErrorReportingThread = isErrorReportingThread;
        this.stacktrace = stacktrace.getTrace();
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public ThreadType getType() {
        return type;
    }

    public boolean isErrorReportingThread() {
        return isErrorReportingThread;
    }

    public void setErrorReportingThread(Boolean errorReportingThread) {
        isErrorReportingThread = errorReportingThread;
    }

    public void setErrorReportingThread(boolean errorReportingThread) {
        isErrorReportingThread = errorReportingThread;
    }

    public void setType(ThreadType type) {
        this.type = type;
    }

    public boolean getErrorReportingThread() {
        return isErrorReportingThread;
    }

    public List<Stackframe> getStacktrace() {
        return stacktrace;
    }

    public void setStacktrace(List<Stackframe> stacktrace) {
        this.stacktrace = stacktrace;
    }

    @Override
    public void toStream(JsonStream writer) throws IOException {
        writer.beginObject();
        writer.name("id").value(id);
        writer.name("name").value(name);
        writer.name("type").value(type.desc);

        writer.name("stacktrace");
        writer.beginArray();

        for (Stackframe stackframe : stacktrace) {
            writer.value(stackframe);
        }

        writer.endArray();

        if (isErrorReportingThread) {
            writer.name("errorReportingThread").value(true);
        }
        writer.endObject();
    }
}
